package com.cxyjy.cfyuiapi.controller;


import com.cxyjy.cfyuiapi.service.AssignTaskService;
import com.cxyjy.cfyuiapi.service.PromptWorkflowService;
import com.cxyjy.cfyuiapi.service.QueryTaskQueueService;
import com.cxyjy.cfyuiapi.service.UploadImageService;
import com.cxyjy.common.util.JFileUtil;
import com.cxyjy.common.util.JObjectUtil;
import com.cxyjy.common.util.JResponse;
import com.cxyjy.common.util.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

import static com.cxyjy.common.constants.CfyuiConstants.BAIDU_EXPECTED_IMAGE_EXTENSIONS;
import static com.cxyjy.common.util.JObjectUtil.*;
import static com.cxyjy.common.util.ResultCode.*;
import static com.cxyjy.common.util.ResultUtil.error;

@RestController
public class PromptController {
    @Autowired
    AssignTaskService assignTaskService;
    @Autowired
    QueryTaskQueueService queryTaskQueueService;
    @Autowired
    PromptWorkflowService promptWorkflowService;
    @Autowired
    UploadImageService uploadImageService;


    /**
     * 小程序要用的视频换脸接口，直接传入图片文件或者图片url即可开始工作流
     *
     * @param image
     * @param url
     * @return
     * @throws IOException
     * @throws IllegalAccessException
     */
    @PostMapping("/generateFaceSwap")
    public JResponse generateFaceSwap(@RequestParam(required = false) MultipartFile image, @RequestParam(required = false) String url) throws IOException, IllegalAccessException {
        //二选一校验：必须传image或url，且不能同时传
        if ((image == null && url == null) || (image != null && url != null)) {
            return error(FACESWAP_EXCLUSIVE_PARAMETERS_PROVIDED);
        }
        if (image != null) {
            if (image.isEmpty() || image.getOriginalFilename() == null) {
                return error(NO_IMAGE_FOUNDED);
            }
            if (!JFileUtil.checkFileExtension(image, BAIDU_EXPECTED_IMAGE_EXTENSIONS)) {
                return error(NO_IMAGE_FOUNDED);
            }
        } else if (!JFileUtil.isImageUrl(url)) {
            return error(NO_IMAGE_FOUNDED);
        }
        String fileName = image != null ? uploadImageService.uploadFaceSwapImage(image) : uploadImageService.uploadFaceSwapImage(url);
        if (!fileName.contains(".")) {
            return error(ResultCode.find(objectToInt(fileName)));
        }
        return promptWorkflowService.runReActorFaceSwap(fileName);
    }

    /**
     * 智慧导览app要用的图片换脸接口，直接传入图片文件或者图片url即可开始工作流
     *
     * 将目标图片(targetImage)中的人脸换为模板图片(sourceImage)中的人脸
     * @param targetImage   目标图片
     * @param sourceImage   模板图片
     * @param targetImageUrl
     * @param sourceImageUrl
     * @return
     * @throws IOException
     */
    @PostMapping("/generateImageFaceSwap")
    public JResponse<?> generateImageFaceSwap(
            @RequestParam(required = false) MultipartFile targetImage,
            @RequestParam(required = false) MultipartFile sourceImage,
            @RequestParam(required = false) String targetImageUrl,
            @RequestParam(required = false) String sourceImageUrl) throws IOException {
        String targetImageName = uploadImageService.uploadImageFaceSwapImageHybrid(targetImage, targetImageUrl);
        String sourceImageName = uploadImageService.uploadImageFaceSwapImageHybrid(sourceImage, sourceImageUrl);
        if(isEmpty(targetImageName) || isEmpty(sourceImageName)){
            return error(FACESWAP_EXCLUSIVE_PARAMETERS_PROVIDED);
        }
        return promptWorkflowService.imageReActorFaceSwap(targetImageName, sourceImageName);
    }


    /**
     * @param fileName
     * @return
     * @throws IOException
     * @throws IllegalAccessException
     */
    @GetMapping("/reActorFaceSwap")
    public JResponse reActorFaceSwap(@RequestParam(name = "filename") String fileName) throws IOException, IllegalAccessException {
        if (JObjectUtil.isEmpty(fileName)) {
            return error(NO_IMAGE_FOUNDED);
        }
        return promptWorkflowService.runReActorFaceSwap(fileName);
    }

    /**
     *
     * @param targetImageName
     * @param sourceImageName
     * @return
     * @throws IOException
     */
    @GetMapping("/imageReActorFaceSwap")
    public JResponse imageReActorFaceSwap(@RequestParam(name = "targetImageName") String targetImageName, @RequestParam(name = "sourceImageName") String sourceImageName) throws IOException {
        if (JObjectUtil.isEmpty(targetImageName) || JObjectUtil.isEmpty(sourceImageName)) {
            return error(NO_IMAGE_FOUNDED);
        }
        return promptWorkflowService.imageReActorFaceSwap(targetImageName, sourceImageName);
    }

    /**
     * 单个视频的ReActorFaceSwap换脸接口，仅用来测试
     *
     * @param fileName
     * @return
     * @throws IOException
     */
    @GetMapping("/singleReActorFaceSwap")
    public JResponse singleReActorFaceSwap(@RequestParam(name = "filename") String fileName) throws IOException, IllegalAccessException {
        if (JObjectUtil.isEmpty(fileName)) {
            return error(PARAMS_ERROR);
        }
        return promptWorkflowService.runSingleReActorFaceSwap(fileName);
    }

    /**
     * 下发文生图绘图任务
     *
     * @param text 用户输入的文字
     * @return
     * @throws IOException
     */
    @PostMapping("/assignTask")
    public JResponse tasksDistribute(@RequestBody String text) throws IOException {
        return assignTaskService.assignTask(text);
    }

    /**
     * 获取服务器当前剩余任务列队的数量
     *
     * @return
     * @throws IOException
     */
    @GetMapping("/queryTaskQueue")
    public JResponse queryTaskQueue() throws IOException {
        return queryTaskQueueService.QueryTaskQueue();
    }

}